package inet.app.view.activity;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.parser.Parser_ListtinhSC;
import inet.app.model.object.ListTinh_Sc_Item;
import inet.app.model.util.Util;
import inet.app.view.adapter.KetQuaDetailAdapter;
import inet.app.view.fragment.FragmentVipCauNgay;
import inet.app.view.fragment.FragmentVipSoiCau;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class VipFragment extends BaseFragmentActivity implements
		OnClickListener {

	private ViewPager _mViewPager;
	private KetQuaDetailAdapter _adapter;
	private FragmentVipSoiCau ftvsc;
	Spinner sptinh;
	private FragmentVipCauNgay ftvcg;
	int Yellow1 = Color.rgb(255, 255, 0);
	int Red1 = Color.rgb(255, 0, 0);
	int heavyblue = Color.rgb(0, 0, 255);
	private ArrayList<Fragment> list_Fragment;
	Dialog about, help, dialog, dlquit;
	int demtrang = 1;
	int max = 10;
	private LinearLayout first_text, second_text;
	private TextView tvTitle, tvNhamang;
	private TextView tvSpTinh;
	private String name = "Thủ Đô", matinh = "XSTD",selectMien="Miền Bắc";
	String[] list_mien = new String[] {" Miền Bắc", " Miền Trung", " Miền Nam"};
	private List<ListTinh_Sc_Item> arraylist = new ArrayList<ListTinh_Sc_Item>();
	private ArrayList<String> list_tentinh = new ArrayList<String>();
	private ArrayList<String> list_matinh = new ArrayList<String>();
	private Parser_ListtinhSC parser = new Parser_ListtinhSC();
	private String json;
	private int posCurrent = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tuvan_fragment);
		initBase();
		initUI();
		initFragment();
		init_ViewPager();
		setTab();
	}

	private void initUI() {
		first_text = (LinearLayout) findViewById(R.id.first_text);
		second_text = (LinearLayout) findViewById(R.id.second_text);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		tvNhamang = (TextView) findViewById(R.id.tvNhamang);
		tvNhamang.setText(Util.getSharePreferences(VipFragment.this));
		first_text.setOnClickListener(this);
		second_text.setOnClickListener(this);
		sptinh = (Spinner) findViewById(R.id.sptinh);
		tvSpTinh = (TextView) findViewById(R.id.tvSpTinh);
		tvSpTinh.setOnClickListener(this);

		getData();

		_mViewPager = (ViewPager) findViewById(R.id.viewPager);
		list_Fragment = new ArrayList<Fragment>();
	}

	private void getData() {
		pdia.show();
		Bundle rb = this.getIntent().getExtras();
		json = rb.getString("json");
		posCurrent = rb.getInt("pos");
		if (json.equals(XoSoConstants.GET_TINH_SC_FROM_DB)) {
			arraylist = mDbManager.getDaoTinhSc().selectAll();
		} else {
			arraylist = parser.getListTinh(json);

			// add new data
			if (arraylist != null && arraylist.size() > 0) {
				mDbManager.getDaoTinhSc().deleteAll();
				mDbManager.getDaoTinhSc().insertCollection(arraylist);
			}
		}
		if (arraylist != null) {
			for (int i = 0; i < arraylist.size(); i++) {
				list_tentinh.add(arraylist.get(i).getName());
				list_matinh.add(arraylist.get(i).getCode());
			}
			pdia.dismiss();
			AddSpinner();
		} else {
			pdia.dismiss();
			Util.showDialog(1, VipFragment.this);
		}

	}

	private void AddSpinner() {
		ArrayAdapter<String> arrayadapter_sptinh = new ArrayAdapter<String>(
				VipFragment.this, android.R.layout.simple_spinner_item,
				list_tentinh);
		arrayadapter_sptinh
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sptinh.setAdapter(arrayadapter_sptinh);
		sptinh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View v,
					final int pos, long id) {
				tvSpTinh.setText(" " + list_tentinh.get(pos));
				sptinh.setSelection(pos);
				matinh = list_matinh.get(pos);
				System.out.println("MA TINH LƯU LẠI " + matinh);
				name = list_tentinh.get(pos);
				((MyApp) getApplication()).setCodeTV(matinh);
				((MyApp) getApplication()).setNameTV(name);
				ftvsc = (FragmentVipSoiCau) _adapter.getItem(0);
				ftvsc.loadItemList(matinh);
				_mViewPager.setCurrentItem(0);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}
	private void AddSpinnerMien() {
		ArrayAdapter<String> arrayadapter_sptinh = new ArrayAdapter<String>(
				VipFragment.this, android.R.layout.simple_spinner_item,
				list_mien);
		arrayadapter_sptinh
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sptinh.setAdapter(arrayadapter_sptinh);
		sptinh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View v,
					final int pos, long id) {
				sptinh.setSelection(pos);
				switch (pos) {
				case 0:
					selectMien = " Miền Bắc";
					break;
				case 1:
					selectMien = " Miền Trung";
					break;
				case 2:
					selectMien = " Miền Nam";
					break;
				default:
					break;
				}
				tvSpTinh.setText(selectMien);
				ftvcg = (FragmentVipCauNgay) _adapter.getItem(1);
				ftvcg.loadSpinner(selectMien);
				_mViewPager.setCurrentItem(1);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}
	private void initFragment() {

		if (posCurrent == 0) {

			ftvsc = (FragmentVipSoiCau) FragmentVipSoiCau.newInstance(this,
					String.valueOf(posCurrent), json, matinh);
			ftvcg = (FragmentVipCauNgay) FragmentVipCauNgay
					.newInstance(this, String.valueOf(posCurrent), json);
		} else if (posCurrent == 1) {

			ftvsc = (FragmentVipSoiCau) FragmentVipSoiCau.newInstance(this,
					String.valueOf(posCurrent), json, matinh);
			ftvcg = (FragmentVipCauNgay) FragmentVipCauNgay
					.newInstance(this, String.valueOf(posCurrent), json);
			first_text.setBackgroundResource(R.drawable.tk_tab1_2);
			second_text.setBackgroundResource(R.drawable.tk_tab2);
		}
		list_Fragment.add(ftvsc);
		list_Fragment.add(ftvcg);
	}

	public void init_ViewPager() {
		FragmentManager fm = getSupportFragmentManager();
		_adapter = new KetQuaDetailAdapter(this, _mViewPager, fm, list_Fragment);
		_mViewPager.setAdapter(_adapter);
		_mViewPager.setCurrentItem(posCurrent);
		first_text.setBackgroundResource(R.drawable.tk_tab1_2);
		second_text.setBackgroundResource(R.drawable.tk_tab2);
		tvSpTinh.setEnabled(true);
		tvTitle.setText("VIP");
	}

	private void setTab() {
		_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int position) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					posCurrent = 0;
					ftvsc = (FragmentVipSoiCau) _adapter.getItem(0);
					first_text.setBackgroundResource(R.drawable.tk_tab1_2);
					second_text.setBackgroundResource(R.drawable.tk_tab2);
					tvTitle.setText("VIP");
					tvSpTinh.setEnabled(true);
					_mViewPager.setCurrentItem(0);
					 AddSpinner();
					break;
				case 1:
					posCurrent = 1;
					ftvcg = (FragmentVipCauNgay) _adapter.getItem(1);
					first_text.setBackgroundResource(R.drawable.tk_tab1);
					second_text.setBackgroundResource(R.drawable.tk_tab2_2);
					tvTitle.setText("Cầu Theo Ngày");
					tvSpTinh.setEnabled(true);
					_mViewPager.setCurrentItem(1);
					AddSpinnerMien();
					break;
				}
			}

		});
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.out.println("THOAT NHE");
			quitDialog(VipFragment.this);
			dlquit.show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.first_text:
			posCurrent = 0;
			ftvsc = (FragmentVipSoiCau) _adapter.getItem(0);
			first_text.setBackgroundResource(R.drawable.tk_tab1_2);
			second_text.setBackgroundResource(R.drawable.tk_tab2);
			tvTitle.setText("VIP");
			tvSpTinh.setEnabled(true);
			_mViewPager.setCurrentItem(0);
			 AddSpinner();
			break;
		case R.id.second_text:
			posCurrent = 1;
			ftvcg = (FragmentVipCauNgay) _adapter.getItem(1);
			first_text.setBackgroundResource(R.drawable.tk_tab1);
			second_text.setBackgroundResource(R.drawable.tk_tab2_2);
			tvTitle.setText("Cầu Theo Ngày");
			tvSpTinh.setEnabled(true);
			_mViewPager.setCurrentItem(1);
			AddSpinnerMien();
			break;
		case R.id.tvSpTinh:
			sptinh.performClick();
			break;
		default:
			break;
		}
	}

	public void quitDialog(Context c) {
		dlquit = new Dialog(c);
		dlquit.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		dlquit.setTitle("Thoát ứng dụng ?");
		dlquit.setContentView(R.layout.dialog_quit);
		dlquit.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		dlquit.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.rgb(50, 50, 50)));
		dlquit.getWindow().setTitleColor(XoSoConstants.White);
		dlquit.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon_registersc);
		
		Button huy = (Button) dlquit.findViewById(R.id.bthuy);
		huy.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				dlquit.dismiss();
			}
		});

		Button quit = (Button) dlquit.findViewById(R.id.btquit);
		quit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((MyApp) getApplication()).setKill(true);
				finish();
				dlquit.dismiss();
			}
		});
	}
}
